package student;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import jdbcutil.JDBCUtil;



public class StudentDao {
	
	Connection conn = null;
	PreparedStatement ps = null;
	
	public Integer insert(StudentModel model) {
		String sql = "insert into student_test.student(id,name,dis)values(?,?,?)";
		try {
			conn = JDBCUtil.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setString(1, model.getId());
			ps.setString(2, model.getName());
			ps.setString(3, model.getDis());	
			return ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(conn, ps);
		}
		return null;
	}
	
	public Integer update(StudentModel model) {
		StringBuffer sql = new StringBuffer("update student_test.student");
		List<Object> vals = appendSet(sql,model);
		try {
			conn = JDBCUtil.getConnection();
			ps = conn.prepareStatement(sql.toString());
			for (int i = 0; i < vals.size(); i++) {
				ps.setObject(i + 1, vals.get(i));
			}
			return ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(conn, ps);
		}
		return null;
	}
	
	private List<Object> appendSet(StringBuffer sql, StudentModel model) {
		sql.append(" set id = id ");
		List<Object> objs = new ArrayList<Object>();

		String name = model.getName();
		if (name != null) {
			sql.append(",name = ? ");
			objs.add(name);
		}
		String dis = model.getDis();
		if (dis != null) {
			sql.append(",dis = ? ");
			objs.add(dis);
		}
		String id = model.getId();
		sql.append("where id = ? ");
		objs.add(id);
		return objs;
	}
	
	public List<StudentModel> selectList(StudentModel model) {
		List<StudentModel> list = new ArrayList<StudentModel>();
		StringBuffer sql = new StringBuffer("select id,name,dis from student_test.student");
		List<Object> vals = appendWhere(sql, model);
		ResultSet rs = null;
		try {
			conn = JDBCUtil.getConnection();
			ps = conn.prepareStatement(sql.toString());
			if (vals.size() == 0) {
				rs = ps.executeQuery();
			} else {
				for (int i = 0; i < vals.size(); i++) {
					ps.setObject(i + 1, vals.get(i));		
				}
				rs = ps.executeQuery();
			}
			while (rs.next()) {
				StudentModel mod = new StudentModel();
				mod.setId(rs.getString("id"));
				mod.setName(rs.getString("name"));
				mod.setDis(rs.getString("dis"));
				list.add(mod);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JDBCUtil.close(conn, ps, rs);
		}
		return list;
	}

	private List<Object> appendWhere(StringBuffer sql, StudentModel model) {
		sql.append(" where 1=1");
		List<Object> objs = new ArrayList<Object>();
		String id = model.getId();
		if (id != null && !id.trim().isEmpty()) {// !"".equals(code.trim())
			sql.append(" and id = ?");
			objs.add(id);
		}
		String name = model.getName();
		if (name != null && !name.trim().isEmpty()) {
			sql.append(" and name = ?");
			objs.add(name);
		}
		String dis = model.getDis();
		if (dis != null && !dis.trim().isEmpty()) {
			sql.append(" and dis = ?");
			objs.add(dis);
		}

		return objs;
	}
	
}
